package com.workerhelping.controller;

import com.workerhelping.dto.ESAssistInfoDTO;
import com.workerhelping.elasticsearchEntity.ESAssistVO;
import com.workerhelping.elasticsearchEntity.ESIncomeVO;
import com.workerhelping.result.Result;
import com.workerhelping.service.ESAssistVOService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ESAssist")
public class ESAssistVOController {

    @Autowired
    private ESAssistVOService esAssistVOService;

    /**
     * 批量插入帮扶信息
     * @param esAssistInfoDTO
     * @return
     */
    @PostMapping("/insertAssistBatch")
    public Result<String> insertAssistBatch(@RequestBody ESAssistInfoDTO esAssistInfoDTO) {
        try {
            List<ESAssistVO> esAssistVOS = esAssistInfoDTO.getEsAssistVOS();
            esAssistVOService.insertBatch(esAssistVOS);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    /**
     * 聚合查询帮扶信息
     * @param startDate
     * @param endDate
     * @param minPrice
     * @param maxPrice
     * @param year
     * @param category
     * @param workerId
     * @return
     */
    @GetMapping("/search")
    public Result<List<ESAssistVO>> searchAssist(@RequestParam(value = "startDate", required = false) String startDate,
                                                 @RequestParam(value = "endDate", required = false) String endDate,
                                                 @RequestParam(value = "minPrice", required = false) Double minPrice,
                                                 @RequestParam(value = "maxPrice", required = false) Double maxPrice,
                                                 @RequestParam(value = "sYear",required = false) String year,
                                                 @RequestParam(value = "sCategory", required = false) String category,
                                                 @RequestParam(value = "workerId", required = false) Integer workerId) {
        try {
            List<ESAssistVO> assists = esAssistVOService.search(startDate, endDate, minPrice, maxPrice,year, category, workerId);
            return Result.ok(assists);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
}
